import { defaultRouter, adminRouters, houseparentRouters, inpectorRouters } from '../../router/index'
const Home = () => import('components/Home.vue')
const state = {
  role: '',
  allRouters: [],
  DefaultRouter: defaultRouter
}
const getters = {
  addrouter: state => state.allRouters
}
const mutations = {
  setRole (context, role) {
    // console.log('set', context)
    context.role = role
    window.localStorage.setItem('role', role)
  },
  moveRole (context, role) {
    context.role = role
  },
  setRouter (context, routers) {
    // console.log('路由长度', routers.length)
    if (routers.length === 0) {
      // console.log('我是学生，添加默认路由')
      // 默认路由拼接一个空的数组，这里是普通用户，也就是学生用户
      context.allRouters = routers.concat(defaultRouter)
      window.localStorage.setItem('router', JSON.stringify(context.allRouters))
    } else {
      if (context.role === 'admin' || context.role === 'general' || context.role === 'inspector') {
        // console.log('我是超级管理员，添加全部路由')
        const Routers = context.role === 'admin' ? adminRouters : context.role === 'general' ? houseparentRouters : inpectorRouters
        const arr = {
          path: '/home',
          component: Home,
          redirect: '/welcome',
          children: routers[3].children.concat(Routers)
        }
        const removeDefaultRouterIndex2 = routers.slice(0, 3)
        context.allRouters = removeDefaultRouterIndex2.concat(arr)
        window.localStorage.setItem('router', JSON.stringify(context.allRouters))
      }
      /**
       * else if (context.role === 'general') {
        // console.log('我是管理员，添加管理路由')
        const arr1 = {
          path: '/home',
          component: Home,
          redirect: '/welcome',
          children: routers[3].children.concat(houseparentRouters)
        }
        const removeDefaultRouterIndex3 = routers.slice(0, 3)
        context.allRouters = removeDefaultRouterIndex3.concat(arr1)
        window.localStorage.setItem('router', JSON.stringify(context.allRouters))
      }
       */
    }
  }
}
const actions = {
  // 获取从后端得到的角色数据
  getRole (context, role) {
    // console.log('get', context)
    if (!context.state.role) {
      // 添加角色到state
      context.commit('setRole', role)
    } else {
      if (context.state.role === role) {
        // throw new Error('角色已存在')
        // console.log('已存在')
        return 0
      } else {
        context.commit('moveRole', null)
        context.commit('setRole', role)
      }
    }
  },
  // 通过localStorage获取角色来添加对应的路由
  getAddRouters (context, routers) {
    // console.log('路由', routers)
    if (window.localStorage.getItem('role')) {
      if (window.localStorage.getItem('role') === 'admin' || window.localStorage.getItem('role') === 'general' || window.localStorage.getItem('role') === 'inspector') {
        // console.log('admin')
        // 这里是默认是获取的默认路由，用来重新和权限路由做拼接
        context.commit('setRouter', routers)
      } else {
        context.commit('setRouter', [])
      }
    } else {
      return false
    }
  }
}
/**
 * else if (window.localStorage.getItem('role') === 'general') {
        context.commit('setRouter', routers)
      }
 */
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
